package obs;

import com.google.gson.Gson;
import com.obs.services.ObsClient;
import com.obs.services.ObsConfiguration;
import com.obs.services.model.ObsObject;
import com.obs.services.model.PutObjectRequest;
import com.obs.services.model.PutObjectResult;

import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * Title: CreateFolderSample
 * Description: 文件夹相关操作
 *
 * @author hfl
 * @version V1.0
 * @date 2020-05-26
 */
public class CreateFolderSample {
    private static final String endPoint = "http://obs.cn-north-4.myhuaweicloud.com";
    private static final String ak = "NNNNWPW2FZHMYPRB3M79OBXXX";
    private static final String sk = "7ZRRRRX0kNN0SgsxtIsB6EBZGygWvpC04mezuT0pbBBBPu";
    private static ObsClient obsClient;
    private static String bucketName = "wuzheng-demo";

    //初始化obs配置
    static {
        ObsConfiguration config = new ObsConfiguration();
        config.setSocketTimeout(30000);
        config.setConnectionTimeout(10000);
        config.setEndPoint(endPoint);
        obsClient = new ObsClient(ak, sk, config);
    }


    public static void main(String[] args) throws IOException {
        //方式1:
        //createEmptyFolder("MyObjectKey1/");
        //isEmptyFolder("MyObjectKey1/");

        //方式2:
        createEmptyFolderBySet();
        isEmptyFolder("MyObjectKey2/");
        closeClient();

    }

    //方式2: set方式
    //创建一个没有请求正文的空文件夹，请注意，密钥必须带有/后缀
    private static void createEmptyFolderBySet() {
        final String keySuffixWithSlash2 = "MyObjectKey2/";
        PutObjectRequest request = new PutObjectRequest();
        request.setBucketName(bucketName);
        request.setObjectKey(keySuffixWithSlash2);
        request.setInput(new ByteArrayInputStream(new byte[0]));
        obsClient.putObject(request);
        System.out.println("Creating an empty folder " + keySuffixWithSlash2 + "\n");
    }




    //方式1: 构造函数方式
    //创建一个没有请求正文的空文件夹，请注意，密钥必须带有/后缀
    private static void createEmptyFolder(String keySuffixWithSlash1) {
//        final String keySuffixWithSlash1 = "MyObjectKey1/";
        PutObjectResult putObjectResult = obsClient.putObject(bucketName, keySuffixWithSlash1, new ByteArrayInputStream(new byte[0]));
        System.out.println(new Gson().toJson(putObjectResult));
        System.out.println("Creating an empty folder " + keySuffixWithSlash1 + "\n");
    }

    //验证是否是文件夹
    private static void isEmptyFolder(String keySuffixWithSlash1) throws IOException {
        //final String keySuffixWithSlash1 = "MyObjectKey1/";
        ObsObject object = obsClient.getObject(bucketName, keySuffixWithSlash1, null);
        System.out.println("Size of the empty folder '" + object.getObjectKey() + "' is " + object.getMetadata().getContentLength());
        object.getObjectContent().close();
    }


    //关闭客户端
    private static void closeClient() {
        if (obsClient != null) {
            try {
                /*
                 * Close obs client
                 */
                obsClient.close();
            } catch (IOException e) {
            }
        }
    }


}
